package com.example.download.widget.loading;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;

import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.LinearInterpolator;

import androidx.annotation.ColorInt;

/**
 * Created by  you
 * Date: 2022/3/30
 * Time: 17:06
 **/
public class CircularRing extends View {
 public final String TAG = getClass().getSimpleName();
 private float mWidth = 0f;
 private float mPadding = 0f;
 private float startAngle = 0f;
 private Paint mPaint;
 private int color = Color.argb(100, 255, 255, 255);
 private Paint mPaint2;

 public CircularRing(Context context) {
 this(context, null);
 }

 public CircularRing(Context context, AttributeSet attrs) {
 this(context, attrs, 0);
 }

 public CircularRing(Context context, AttributeSet attrs, int defStyleAttr) {
 super(context, attrs, defStyleAttr);
 initPaint();
 }

 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 super.onMeasure(widthMeasureSpec, heightMeasureSpec);

 if (getMeasuredWidth() > getHeight())
 mWidth = getMeasuredHeight();
 else
 mWidth = getMeasuredWidth();
 mPadding = 5;
 }

 @Override
 protected void onDraw(Canvas canvas) {
 super.onDraw(canvas);

 //        mPaint.setColor(Color.argb(100, 255, 255, 255));
 mPaint2.setColor(color);
 canvas.drawCircle(mWidth / 2, mWidth / 2, mWidth / 2 - mPadding, mPaint2);
 mPaint.setColor(Color.WHITE);
 RectF rectF = new RectF(mPadding, mPadding, mWidth - mPadding, mWidth - mPadding);
 canvas.drawArc(rectF, startAngle, 100
 , false, mPaint);//第四个参数是否显示半径
 }


 private void initPaint() {
 mPaint = new Paint();
 mPaint.setAntiAlias(true);
 mPaint.setStyle(Paint.Style.STROKE);
 mPaint.setColor(color);
 mPaint.setStrokeWidth(8);

 mPaint2 = new Paint();
 mPaint2.setAntiAlias(true);
 mPaint2.setStyle(Paint.Style.STROKE);
 mPaint2.setStrokeWidth(8);
 mPaint2.setColor(color);
 }

 public void startAnim() {
 stopAnim();
 startViewAnim(0f, 1f, 1000);
 }

 public void stopAnim() {
 if (valueAnimator != null) {
 clearAnimation();
 valueAnimator.setRepeatCount(1);
 valueAnimator.cancel();
 valueAnimator.end();
 }
 }

 ValueAnimator valueAnimator;

 private ValueAnimator startViewAnim(float startF, final float endF, long time) {
 valueAnimator = ValueAnimator.ofFloat(startF, endF);

 valueAnimator.setDuration(time);
 valueAnimator.setInterpolator(new LinearInterpolator());
 valueAnimator.setRepeatCount(ValueAnimator.INFINITE);//无限循环
 valueAnimator.setRepeatMode(ValueAnimator.RESTART);//

 valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
 @Override
 public void onAnimationUpdate(ValueAnimator valueAnimator) {

 float value = (float) valueAnimator.getAnimatedValue();
 startAngle = 360 * value;

 invalidate();
 }
 });
 valueAnimator.addListener(new AnimatorListenerAdapter() {
 @Override
 public void onAnimationEnd(Animator animation) {
 super.onAnimationEnd(animation);
 }
 });
 if (!valueAnimator.isRunning()) {
 valueAnimator.start();
 }

 return valueAnimator;
 }

 public void setColor(@ColorInt int color) {
 this.color = color;
 mPaint.setColor(color);
 mPaint2.setColor(color);
 }
 }



